<?php
namespace Home\Controller;

use Think\Controller;

class BankController extends CommonController{

//银行管理首页显示
    public function index(){
      
    $con = array('isdel'=>'0');
    if($_GET['p']==''){
        $p='1';
    }else{
        $p=$_GET['p'];
    }
    $count=D('Bank')->countBank($con);
    $n=15;
    $page=new \Think\Page($count,$n);
    $show= $page->show();// 分页显示输出
    $this->assign('page',$show);// 赋值分页输出
    //$banks = M('bank')->where($con)->page($p.',10')->select();
      $BankNum = D('Bank') -> countBank($con);
      $this->assign('BankNum',$BankNum);
      $banks = D('Bank') -> selectBank($con,$p,$n);
      $this->assign('banks' , $banks);
      $this->display('Bank/index');
    }

    public function search(){
      if(I('post.bankname')){
        $bank = M('bank') ->where( array('bank_name'=>array('like','%'.I('post.bankname').'%') , 'pid'=>'0') )->find();
        if($bank){
            $con['isdel']='0';
            $con['bank_id|pid'] = $bank['bank_id'];
        }
        else{
            $con = array('isdel'=>'0' , 'bank_name'=>array('like','%'.I('post.bankname').'%'));
        }
        
      }
      else{
        $con = array('isdel'=>'0');
      }
      if($_GET['p']==''){
        $p='1';
      }else{
        $p=$_GET['p'];
      }
      $count=D('Bank')->countBank($con);
      $n=15;
      $page=new \Think\Page($count,$n);
      $show= $page->show();// 分页显示输出
      $this->assign('page',$show);// 赋值分页输出
      $BankNum = D('Bank') -> countBank($con);
      $this->assign('BankNum',$BankNum);
      $banks = D('Bank') -> selectBank($con,$p,$n);
      $this->assign('banks' , $banks);
      $this->assign('searname' ,I('post.bankname'));
      $this->display('Bank/index');
    }

//银行添加页面
    public function bankadd(){
      $this -> assign('place' , C('place')) ;
      $banks = D('Bank') -> selectBank(array('pid'=>'0','isdel'=>'0'));
      $this->assign('banks' , $banks);
      $this->display('Bank/bank-add');
    }

//银行修改
    public function bankalert(){
      $id = I('get.id');
      $bank = D('Bank')->findData($id);
      $this->assign('bank',$bank);
      $this -> assign('place' , C('place')) ;
      $banks = D('Bank') -> selectBank(array('pid'=>'0','isdel'=>'0'));
      $this->assign('banks' , $banks);
      $this->display('Bank/bank-alert');
    }

//银行添加检验
    public function addCheck(){
      if(IS_POST){
        $point_id = D('Point') -> addPoint('Bank/addCheck', '添加银行');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
          $data['bank_name'] = I('post.bankname');
          $data['pid'] = I('post.pid');
          $data['place'] = '1';//
          if($data['bank_name'] && $data['place']){
            $findBankName = D('Bank') -> findSameBank($data['bank_name'],$data['pid']);
            if($findBankName){
              $this -> jsonReply('500' , '此银行已被添加');
            }
            $add = D('Bank') -> add($data);
            if($add){
              $this -> jsonReply('200' , '添加成功');
            }
            else{
              $this -> jsonReply('500' , '添加失败');
            }
          }
          else{
            $this -> jsonReply('500' , '银行名称，所属地区不能为空');
          }
        }
        else{
          $this -> jsonReply('500' , '您没有权限操作此功能');
        }
      }
    }

//银行修改检验
public function editCheck(){
  if(IS_POST){
    $point_id = D('Point') -> addPoint('Bank/editCheck', '编辑银行');//功能节点id
    $roleid = session('USER_AUTH.roleid');//权限角色id
    if( haveAblitity($point_id , $roleid) ){
      $bankid = I('post.bankid');
      $data['bank_name'] = I('post.bankname');
      $data['pid'] = I('post.pid');
      $data['place'] = '1';//
      if($data['bank_name'] && $data['place']){
        $findBankName = D('Bank') -> findSameBank($data['bank_name'],$data['pid'],$bankid);
        if($findBankName){
          $this -> jsonReply('500' , '此银行已被添加');
        }
        $add = D('Bank') -> saveBank($bankid,$data);
        if($add){
          $this -> jsonReply('200' , '修改成功');
        }
        else{
          $this -> jsonReply('500' , '您没有对银行进行修改');
        }
      }
      else{
        $this -> jsonReply('500' , '银行名称，所属地区不能为空');
      }
    }
    else{
      $this -> jsonReply('500' , '您没有权限操作此功能');
    }
  }
}

//银行删除操作
public function delCheck(){
  if(IS_POST){
    $point_id = D('Point') -> addPoint('Bank/delCheck', '删除银行');//功能节点id
    $roleid = session('USER_AUTH.roleid');//权限角色id
    if( haveAblitity($point_id , $roleid) ){
      $id = I('post.bankid');
      $del = D('Bank') -> saveBank($id,array('isdel'=>'1'));
      if($del){
        $this -> jsonReply('200' , '删除成功');
      }
      else{
        $this -> jsonReply('500' , '删除失败');
      }
    }
    else{
      $this -> jsonReply('500' , '您没有权限操作此功能');
    }
  }
}

//批量删除的检验
    public function delDatas(){
      if(IS_POST){
        $point_id = D('Point') -> addPoint('Bank/delDatas', '批量删除银行');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
          $id = I('post.id');
          if(count($id)==1){
            $del = D('Bank') -> saveBank($id[0],array('isdel'=>'1'));
          }
          if(count($id)>1){
            foreach($id as $k=>$v){
              $del = D('Bank') -> saveBank($v,array('isdel'=>'1'));
            }
          }
          if($del){
            $this -> jsonReply('200' , '删除成功');
          }
          else{
            $this -> jsonReply('500' , '删除失败');
          }
        }
        else{
          $this -> jsonReply('500' , '您没有权限操作此功能');
        }
      }
    }








}
